package com.easylinkin.linkappapi.facerecognition.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easylinkin.linkappapi.facerecognition.entity.RecognitionRecord;
import com.easylinkin.linkappapi.facerecognition.mapper.RecognitionRecordMapper;
import com.easylinkin.linkappapi.facerecognition.service.RecognitionRecordService;
import com.easylinkin.linkappapi.security.context.LinkappUserContextProducer;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class RecognitionRecordServiceImpl extends ServiceImpl<RecognitionRecordMapper, RecognitionRecord> implements RecognitionRecordService {

    @Resource
    private LinkappUserContextProducer linkappUserContextProducer;

    @Override
    public IPage<RecognitionRecord> queryRecognitionRecordList(Page<RecognitionRecord> page, RecognitionRecord record) {
        QueryWrapper<RecognitionRecord> qw = new QueryWrapper<>();
        qw.eq("tenant_id", linkappUserContextProducer.getNotNullCurrent().getTenantId());

        if (record.getRecognitionType() != null) {
            qw.eq("recognition_type", record.getRecognitionType());
        }

        if (record.getDeviceCode() != null && record.getDeviceCode().trim().length() > 0) {
            qw.eq("device_id", record.getDeviceCode());
        }

        if (record.getName() != null && record.getName().trim().length() > 0) {
            qw.like("recognition_name", "%" + record.getName().trim() + "%");
        }

        if (record.getPersonnelId() != null) {
            qw.eq("recognition_personnel", record.getPersonnelId());
        }

        if (record.getQueryTimeStart() != null) {
            qw.ge("create_time", record.getQueryTimeStart());
        }

        if (record.getQueryTimeEnd() != null) {
            qw.le("create_time", record.getQueryTimeEnd());
        }

        qw.orderByDesc("create_time");

        return baseMapper.selectPage(page, qw);
    }
}
